package com.zzyl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingPlanProject;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {
    @Autowired
    NursingPlanMapper nursingPlanMapper;

    /**
     * 获得所有计划
     * @return
     */
    @Override
    public List<NursingPlanVo> getAllPlan() {
        return nursingPlanMapper.getAll();
    }

    /**
     * 添加计划
     * @param nursingPlanDto
     */
    @Override
    @Transactional
    public void addPlan(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan=new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto,nursingPlan);

        //status不能为空 给status赋初值 默认1是启用状态
        nursingPlan.setStatus(1);

        //先插入计划 回显id
        nursingPlanMapper.addPlan(nursingPlan);


        //将每个项目的planId赋值 赋的值时plan的id
        List<NursingPlanProject> projectPlans = nursingPlan.getProjectPlans();
        for (NursingPlanProject projectPlan : projectPlans) {
            projectPlan.setPlanId(nursingPlan.getId());
        }



        nursingPlanMapper.addPlanProject(projectPlans);
    }

    /**
     * 分页查询
     * @param name
     * @param pageNum
     * @param pageSize
     * @param status
     * @return
     */
    @Override
    public PageResponse<NursingPlanVo> pageQuery(String name, Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum,pageSize);
        Page nursingPlanPage=nursingPlanMapper.queryPage(name,status);
        return PageResponse.of(nursingPlanPage,NursingPlanVo.class);
    }

    /**
     * 根据id查询plan和planProjects
     * @param id
     * @return
     */
    @Override
    @Transactional
    public NursingPlanVo getByPlanId(Integer id) {
        //先查nursingplan
        NursingPlanVo nursingPlanVo= nursingPlanMapper.getPlanByPlanId(id);
        //再查nurisngplanproject
        List<NursingPlanProject> nursingPlanProjectList= nursingPlanMapper.getProjectPlansByPlanId(id);
        System.out.println(nursingPlanProjectList);
        nursingPlanVo.setProjectPlans(nursingPlanProjectList);
        return nursingPlanVo;
    }

    /**
     * 编辑护理计划
     * @param nursingPlanDto
     */
    @Override
    @Transactional
    public void updateNursingPlan(NursingPlanDto nursingPlanDto) {
        //先更改plan
        NursingPlan nursingPlan=new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto,nursingPlan);
        nursingPlanMapper.updatePlan(nursingPlan);

        //再更改planproject(批量更改) 1删除所有
        nursingPlanMapper.deleteNursingPorjectByPlanId(nursingPlanDto.getId());
        //重新添加

        //将每个项目的planId赋值 赋的值时plan的id
        List<NursingPlanProject> projectPlans = nursingPlan.getProjectPlans();
        for (NursingPlanProject projectPlan : projectPlans) {
            projectPlan.setPlanId(nursingPlan.getId());
        }
        //新增护理项目
        nursingPlanMapper.addPlanProject(projectPlans);


    }

    @Override
    @Transactional
    public void deleteBursingPlan(Integer id) {
        //删除计划
        nursingPlanMapper.deleteNursingPlanById(id);
        //删除计划的项目
        Long lId=id.longValue();
        nursingPlanMapper.deleteNursingPorjectByPlanId(lId);
    }

    @Override
    public void setStatus(Integer id, Integer status) {
        nursingPlanMapper.setStatus(id,status);
    }
}
